﻿using System;
using System.Text;
using System.Xml;

namespace APIDataManage
{
    public class ExtrasXml
    {
        /// <summary>
        /// Extra insert into DBTable
        /// </summary>
        public void insertXmlToDB()
        {
            APIDataFunction api = new APIDataFunction();
            XmlDocument doc = api.getAllExtra();
            if (doc != null)
            {
                XmlElement root = doc.DocumentElement;
                StringBuilder sb = new StringBuilder();

                ////IF NOT EXISTS(SELECT name FROM t1 WHERE name='John Smith')

                foreach (XmlNode node in root.ChildNodes)
                {
                    sb.AppendFormat("if not exists (select id_opt from Extras where id_opt = " + node.Attributes["id_opt"].Value + ") insert into  Extras (id_opt, value, createddate, updatedatetime) values({0},'{1}', '{2}', '{3}');", 
                        node.Attributes["id_opt"].Value, node.InnerText, DateTime.Now.ToString(), DateTime.Now.ToString());
                }
                if (sb.Length > 0)
                {
                    SqlDbHelper.ExecteNonQuery(sb.ToString(), null);
                }
            }
        }
    }
}
